package com.module.admin.log.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;

import com.module.admin.BaseController;
import com.module.admin.prj.service.PrjInfoService;
import com.ms.MsConfig;
import com.ms.env.Env;
import com.ms.env.EnvUtil;
import com.system.auth.AuthUtil;
import com.system.comm.model.KvEntity;
import com.system.comm.utils.FrameTimeUtil;

/**
 * prj_api的Controller
 * @author yuejing
 * @date 2016-11-30 13:30:00
 * @version V1.0.0
 */
@Controller
public class LogInfoController extends BaseController {

	@Autowired
	private PrjInfoService prjInfoService;
	
	@InitBinder
	public void initBinder(WebDataBinder binder) {
		try {
			SimpleDateFormat dateFormat = new SimpleDateFormat(FrameTimeUtil.FMT_DEFAULT);
			dateFormat.setLenient(false);
			//true:允许输入空值，false:不能为空值
			binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
		}catch(Exception e) {
			SimpleDateFormat dateFormat = new SimpleDateFormat(FrameTimeUtil.FMT_YYYY_MM_DD);
			dateFormat.setLenient(false);
			//true:允许输入空值，false:不能为空值
			binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
		}
	}

	/**
	 * 跳转到管理页
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/logInfo/f-view/manager")
	public String manger(HttpServletRequest request, ModelMap modelMap) {
		//获取所有服务
		List<KvEntity> prjInfos = prjInfoService.findKvAll();
		modelMap.put("prjInfos", prjInfos);
		return "admin/log/info-manager";
	}

	@RequestMapping(value = "/logInfo/f-view/traces")
	public String traces(HttpServletRequest request, ModelMap modelMap,
			String id) {
		String time = String.valueOf(System.currentTimeMillis());
		//跳转到查看调度链
		String host = EnvUtil.get(Env.SPRING_ZIPKIN_UI_HOST);
		String authString = "?clientId=" + MsConfig.getClientId() + "&time=" + time
			+ "&sign=" + AuthUtil.auth(MsConfig.getClientId(), time, MsConfig.getSercret());
		return "redirect:" + host + "/traces/" + id + authString;
	}

	/*@RequestMapping(name = "获取日志", value = "/logInfo/f-json/find")
	@ApiInfo(params = {
			@ApiParam(name="key的值[ms-cloud-log.INFO/ms-cloud-log.ERROR]", code="key", clazz=String.class, value="INFO"),
			@ApiParam(name="开始日期", code="beginTime", clazz=String.class, value="2017-10-24 12:12:12"),
			@ApiParam(name="开始日期", code="endTime", clazz=String.class, value="2017-10-25 12:12:12")
	},
	response = {
			@ApiRes(name="响应码", code="code", clazz=String.class, value="0"),
			@ApiRes(name="响应消息", code="message", clazz=String.class, value="success"),
			@ApiRes(name="主体内容", code="body", clazz=List.class, value=""),
			@ApiRes(name="编号", code="traceId", pCode="body", clazz=String.class, value=""),
			@ApiRes(name="key的内容", code="aKey", pCode="body", clazz=String.class, value=""),
			@ApiRes(name="key的值", code="aValue", pCode="body", clazz=String.class, value=""),
			@ApiRes(name="添加时间", code="aTime", pCode="body", clazz=String.class, value="")
	})
	public ResponseFrame find(HttpServletRequest request, HttpServletResponse response,
			String key, Date beginTime, Date endTime) {
		ResponseFrame frame = new ResponseFrame();
		try {
			//frame = zipkinAnnotationsService.updateHeartbeat(clientId);
			//Date beginTime = FrameTimeUtil.parseDate("2017-10-22 10:10:10", "yyyy-MM-dd HH:mm:ss");
			//Date endTime = FrameTimeUtil.parseDate("2017-10-23 10:10:10", "yyyy-MM-dd HH:mm:ss");
			List<ZipkinAnnotations> list = zipkinAnnotationsService.findByAKeyATimestamp(key, beginTime, endTime);
			frame.setBody(list);
			frame.setSucc();
		} catch (Exception e) {
			LOGGER.error("获取日志异常: " + e.getMessage(), e);
			frame = new ResponseFrame(ResponseCode.FAIL);
		}
		return frame;
	}*/
}